//
// Created by henry on 2025/11/7.
//

#include "front_user.h"

//登陆
int front_user_login(){
    FrontUser user;
    printf("输入用户名:");
    scanf("%s",user.username);
    printf("输入密码:");
    scanf("%s",user.passowrd);

    //查询该账号是否存在
    //定义查询sql
    //select * from front_user where username=? and password=?;
    //select count(*) from front_user where username=? and password=?;
    const char * sql =
            "select count(*) from front_user where username=? and password=?";
    //定义预编译语句句柄
    sqlite3_stmt * stmt;
    int rc = sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"front_user_login:编译出错!\n");
        return -1;
    }
    //绑定参数: 注意?位置，从1开始
    sqlite3_bind_text(stmt,1,user.username,-1,SQLITE_TRANSIENT);
    sqlite3_bind_text(stmt,2,user.passowrd,-1,SQLITE_TRANSIENT);
    //执行查询:在表中读一次记录，会返回读取的状态:
    // SQLITE_ROW 说明读取到了，此时就可以获取改行的值
    // SQLITE_DONE 或者其他状态 说明没读取到，就不能去获取值

    rc = sqlite3_step(stmt);
    //查询的行数
    int rowcount = 0;
    //读取到一行数据
    if(rc==SQLITE_ROW){
        //获取改行的列: 注意位置，列都是从0开始
        rowcount = sqlite3_column_int(stmt,0);
    }

    if (rowcount==0){
        printf("该账号不存在,登陆失败!\n");
        return -1;
    }
    printf("账号存在，登陆成功!\n");
    return 0;
}
//注册
int front_user_regist(){
    printf("前端用户注册!\n");
}